package com.example.yuda.myserviceapplication;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

/**
 * 第一种启动方式：startService
 * 1 创建一个服务，继承service
 * 2 声明服务
 * 3 启动服务
 * 4 与startService对应的是stopService
 */
public class MainActivity extends Activity implements OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化控件
        Button btnStart = (Button) findViewById(R.id.button1);
        Button btnStop = (Button) findViewById(R.id.button2);
        //添加监听事件
        btnStart.setOnClickListener(this);
        btnStop.setOnClickListener(this);
        findViewById(R.id.bt_play).setOnClickListener(this);
        findViewById(R.id.bt_stop).setOnClickListener(this);
        findViewById(R.id.bt_pause).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button1://启动服务
                Intent in = new Intent(MainActivity.this, MyService.class);
                startService(in);
                break;

            case R.id.button2://停止服务
                Intent in2 = new Intent(MainActivity.this, MyService.class);
                stopService(in2);
                break;
            case R.id.bt_play:
                startMyService(1);
                break;
            case R.id.bt_pause:
                startMyService(2);
                break;
            case R.id.bt_stop:
                startMyService(3);
                break;
            default:
                break;
        }
    }
    private void startMyService(int action){
        Intent in = new Intent(this, MyService.class);
        in.putExtra("action", action);
        startService(in);
    }
}